package view.components;

import filter.IFilter;
import graphs.AbstractGraph;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Properties;
import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import model.IGedivaModel;
import model.Model;
import view.GedivaGUI;

public class MenuBar extends JMenuBar
{
    private static final long serialVersionUID = 1L;
    private Properties myConstants;
    private GedivaGUI myGUI;
    private JMenu myGraphMenu;
    private JMenu myFilterMenu;

    /**
     * make menu bar
     * 
     * @param constants
     * @param gui
     */
    public MenuBar(Properties constants, GedivaGUI gui)
    {
        myConstants = constants;
        myGUI = gui;

        // make editable menus
        myGraphMenu = makeGraphMenu();
        myFilterMenu = makeFilterMenu();

        // add menus in order
        this.add(makeFileMenu());
        this.add(myGraphMenu);
        this.add(myFilterMenu);
        this.add(makeHelpMenu());
    }

    /**
     * Create the file menu for the program.
     */
    private JMenu makeFileMenu()
    {
        JMenu result = new JMenu(myConstants.getProperty("FILE"));

        // Open a new window
        result.add(new AbstractAction(myConstants.getProperty("NEW_WINDOW"))
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev)
            {
                IGedivaModel model1 = new Model();
                new GedivaGUI(model1);
            }
        });

        // Open a File
        result.add(new AbstractAction(myConstants.getProperty("OPEN"))
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev)
            {
                final JFileChooser open = new JFileChooser(new File(myConstants
                        .getProperty("DEFAULT_DATA_DIRECTORY")));

                // add action listener
                open.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent ev)
                    {
                        try
                        {
                            myGUI.processFile(open.getSelectedFile().toURI()
                                    .toURL());
                        }
                        catch (MalformedURLException e)
                        {
                            myGUI.showMessage(e.getMessage());
                        }
                    }
                });

                // allow open file to be clicked
                open.showDialog(null, "Process Data File");
            }
        });

        // Save Graph
        result.add(new AbstractAction(myConstants.getProperty("SAVE"))
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev)
            {
                myGUI.showMessage("save me!");
            }
        });

        // Show/Hide Side Panel
        result.add(new AbstractAction(myConstants.getProperty("PANEL_VISIBLE"))
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev)
            {
                myGUI.togglePanelVisibility();
            }
        });

        // Close Current Window
        result.add(new AbstractAction(myConstants.getProperty("QUIT"))
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev)
            {
                myGUI.setVisible(false);
            }
        });

        // Quit ALL Windows
        result.add(new AbstractAction(myConstants.getProperty("QUIT_ALL"))
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev)
            {
                System.exit(0);
            }
        });

        return result;
    }

    /**
     * Create the help menu for the program.
     */
    private JMenu makeHelpMenu()
    {
        JMenu result = new JMenu(myConstants.getProperty("HELP"));

        result.add(new AbstractAction(myConstants.getProperty("ABOUT"))
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev)
            {
                myGUI.showMessage(myConstants.getProperty("PEOPLE"));
            }
        });

        return result;
    }

    /**
     * Create the help graph for the program, which will be updated to show the
     * available graph types
     */
    private JMenu makeGraphMenu()
    {
        JMenu result = new JMenu(myConstants.getProperty("GRAPH_MENU"));

        result.add(myConstants.getProperty("DEFAULT_TEXT"));

        return result;
    }

    /**
     * Create the help filter for the program, which will be updated to show the
     * available filters
     */
    private JMenu makeFilterMenu()
    {
        JMenu result = new JMenu(myConstants.getProperty("FILTER_MENU"));

        result.add(myConstants.getProperty("DEFAULT_TEXT"));

        return result;
    }

    /**
     * Add the given graph Command as a menu option with the given name.
     */
    public void addGraphType(final AbstractGraph<?, ?> iGraph)
    {

        myGraphMenu.add(new AbstractAction(iGraph.toString())
        {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent ev)
            {
                myGUI.drawGraph(iGraph);
            }
        });
    }

    /**
     * Add the given graph Command as a menu option with the given name.
     */
    public void addFilter(final IFilter<?, ?> iFilter)
    {
        JCheckBox box = new JCheckBox(iFilter.toString());
        box.addActionListener(new ActionListener()
        {
            private boolean isChecked = false;

            public void actionPerformed(ActionEvent e)
            {
                if (isChecked)
                {
                    myGUI.removeFilter(iFilter);
                }
                else
                {
                    myGUI.applyFilter(iFilter);
                }
                isChecked = !isChecked;
            }
        });
        myFilterMenu.add(box);
    }

    /**
     * clears the available graph types menu
     */
    public void clearGraphs()
    {
        myGraphMenu.removeAll();
    }

    /**
     * clears the available filters menu
     */
    public void clearFilters()
    {
        myFilterMenu.removeAll();
    }
}
